//shopcart模块

import { reqCarList,reqDeleteCartById,reqUpdateCheckedById } from "@/api";
import { Promise } from "core-js";

//state:仓库存储数据的地方
const state = {
    carList:[]
};
//mutations:修改state的唯一手段
const mutations = {
    GETCARLIST(state,carList){
        state.carList = carList
    }
};
//actions:处理action，可以书写自己的业务逻辑，也可以处理异步
const actions = {
    //获取购物车列表
    async getCarList({ commit }) {
        let result = await reqCarList()
        if(result.code==200){
            commit("GETCARLIST",result.data)
        }
    },
    //删除购物车的某一个产品
   async deleteCartListBySkuId({commit},skuId){
        let result = await reqDeleteCartById(skuId)
        if(result.code==200){
            return "ok"
        }else{}
        return Promise.reject(new Error("faile"))
    },
    //修改购物车某一个产品的选中状态
   async updateCheckedById({commit},{skuId,isChecked}){
        let result = await reqUpdateCheckedById(skuId,isChecked)
        if(result.code==200){
            return "ok"
        }else{}
        return Promise.reject(new Error("faile"))
    }
};
//getters:可以理解为计算属性，用于简化仓库数据，让组件获取仓库数据更加方便
//主要用于简化仓库数据
const getters = {
    carList(state){
        return state.carList[0]||{}
    },
};

export default {
    state,
    mutations,
    actions,
    getters,
}